<?php

namespace api\libs\cdb;
/**
 * Cdb帮助类
 *
 * @author johnson
 */
class CdbHelper {

    /**
     * 产生随机字符串
     * @param int $length
     * @return string 长度为$length的字符串
     */
    private static function _random($length) {
        $hash = '';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        for ($i = 0; $i < $length; $i++) {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

    /**
     * 产生随机串
     * @param int $length 必需为偶数
     * @return string
     */
    public static function generateKey($length = 64) {
        $random = self::_random($length / 2);
        $info = md5($_SERVER['SERVER_SOFTWARE'] . $_SERVER['SERVER_NAME'] . $_SERVER['SERVER_ADDR'] . $_SERVER['SERVER_PORT'] . $_SERVER['HTTP_USER_AGENT'] . time());
        $return = array();
        for ($i = 0; $i < $length / 2; $i++) {
            $return[$i] = $random[$i] . $info[$i];
        }
        return implode('', $return);
    }

}

?>
